{extend name="Public:base"/}

{block name="main"}
<link rel="stylesheet" href="/public/static/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="/public/static/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/public/static/ztree/js/jquery.ztree.excheck.js"></script>

<div class="box-info">
    <form id="myform" name="myform" action="{:U('Role/access_edit')}" method="post">
        <input type="hidden" id="group_id" name="group_id" value="{$group_id}" />

        <input type="hidden" name="rules" id="rules" value="{$rules}">
        <ul id="treeDemo" class="ztree" data-url="{:U('Role/access', ['roleid'=>$group_id])}"></ul>

        <div class="card-footer">
            <input type="hidden" name="isajax" value="1" />
            <input name="dosubmit" type="submit" value="确认" class="btn btn-primary">
            <button type="button" class="btn btn-danger" onclick="javascript:history.back(-1);return false;">返回</button>
        </div>
    </form>
</div>

{/block}

{block name="script"}
<script type="text/javascript">
    var setting = {
            check: {
                enable: true
            },
            async: {
                enable: true,
                url: $('#treeDemo').data('url'),
        },
        data: {
            key: {
                children: "children",
                name: "title",
            },
            simpleData: {
                enable : true,
                idKey : "id", // id编号命名
                pIdKey : "pid", // 父id编号命名
                rootId : 0
            }
        },
        callback:{
            beforeCheck:true,
            onCheck:onCheck
        }
    };

    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting);
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            type = { "Y" : "ps", "N" : "ps" };
        zTree.setting.check.chkboxType = type;
    });

    function onCheck(e,treeId,treeNode){
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes=treeObj.getCheckedNodes(true),
            v="";
        for(var i=0;i<nodes.length;i++){
            v+=nodes[i].id + ",";
        }
        $('#rules').val(v);
    }

    $(document).ready(function() {
        $('#myform').ajaxForm({
            success:  complete,  // post-submit callback
            dataType: 'json'
        });
    });

    function complete(data){
        if (data.status == 1) {
            layer.msg(data.info, {
                icon: 1,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            }, function(){
                window.location.href = "{$Think.cookie.__forward__}";
                return true;
            });
        } else {
            layer.msg(data.info, {
                icon: 2,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            });
        }
    }
</script>

{/block}